package cn.hwadee.muying.controller.sys;


import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.commons.utils.JsonUtils;
import cn.hwadee.muying.entity.Role;
import cn.hwadee.muying.service.RoleService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tony
 * @since 2018-04-17
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @ModelAttribute("role")
    public Role get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return roleService.selectById(id);
        } else {
            return new Role();
        }
    }

    //@RequiresPermissions("sys:role:list")
    @RequestMapping(value = "/list", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/sys/role/list";
    }

    @ResponseBody
    //@RequiresPermissions("sys:role:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public GridResult list() throws Exception{
        MyPage<Role> page = getPage();
        page = roleService.list(page,getCurrentUser());
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    //@RequiresPermissions("sys:role:edit")
    @RequestMapping(value = "/edit", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String edit(@ModelAttribute Role role, Model model) throws Exception{
        model.addAttribute("role",role);
        return "/sys/role/edit";
    }

    //@RequiresPermissions("sys:role:perm")
    @RequestMapping(value = "/perm", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String perm(@ModelAttribute Role role, Model model) throws Exception{
        model.addAttribute("permTree", JsonUtils.beanToJson(roleService.selectPermTree(role.getPerms(),getCurrentUser())));

        return "/sys/role/perm";
    }
    /*
    @RequiresPermissions("sys:role:seller")
    @RequestMapping(value = "/seller", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String seller(@ModelAttribute Role role, Model model) throws Exception{
        model.addAttribute("sellerTree", JsonUtils.beanToJson(roleService.selectSellerTree(role.getSeller(),getCUser())));
        return "/sys/role/seller";
    }
    */
    /**
     * 更新角色信息
     * @param role
     * @return
     * @throws Exception
     */
    //@RequiresPermissions(value = {"sys:role:edit","sys:role:perm","sys:role:seller"}, logical = Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Role role){
        Map<String,Object> result = new HashedMap();
        try {
            if(role.getId() != null && role.getId() > 0){
                roleService.updateById(role);
            }else {
                roleService.insertRole(role,getCurrentUser());
            }
            result.put("status",200);
            result.put("msg","角色信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","角色信息保存失败");
        }
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     * @throws Exception
     */
    //@RequiresPermissions("sys:role:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashedMap();
        try {
            if(id != null && id > 0){
                roleService.deleteById(id);
                result.put("status",200);
                result.put("msg","删除角色成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的角色");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除角色失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/getRoles", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Map<String,Object> getRoles(Long orgId){
        Map<String,Object> result = new HashedMap();
        try{
            result.put("roles",roleService.getByOrgId(orgId));
            result.put("msg","获取角色列表成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","获取角色列表失败");
        }
        return result;
    }
}

